package com.inviscidlabs.schooled;


import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.Menu;

public class ActivitySchooled extends FragmentActivity implements FragmentSchooledHome.HomeItemListener{

// Misc Variables
	
	private GradeDB dB;
	private SharedPreferences sP;
	
//ID's and numbers
	private long activeSemester;
	

//Request Code
	private static final int REQ_LAUNCH_SEMEDIT_NOSEMESTERS=229;
	private static final int REQ_LAUNCH_CLASSEDIT_NOCLASSES=228;
	private static final int REQ_LAUNCH_ADDGRADE=227;
	private static final int REQ_LAUNCH_BAREMINIMUM=226;
	private static final int REQ_LAUNCH_GRADEBOOK=225;
	
	private static final int REQ_LAUNCH_SETTINGS=219;
	private static final int REQ_LAUNCH_ADDCLASS=218;
	private static final int REQ_LAUNCH_VIEWCLASSES=217;
	private static final int REQ_LAUNCH_VIEWTYPES=216;
	
	
	
//==================ONCREATE=============================
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity__schooled);
		dB=GradeDB.getInstance(this);
		sP=getSharedPreferences(LeModule.PREFS, Activity.MODE_PRIVATE);
		checkSemesters();
		
	}
	
	
	
//===================ON CREATE ACTIONBAR===========================
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_schooled, menu);
		return true;
	}



//=========BUTTON CLICKED ON HOME SCREEN=================
	@Override
	public void buttonClicked(int bId) {
		switch(bId){
		case R.id.schooled_btn_addGrade:
			//TODO launch gradeEdit 
		break;
		
		case R.id.schooled_btn_bareminimum:
			//TODO launch bareminimum
		break;
		
		case R.id.schooled_btn_viewgrades:
			//TODO launch gradebook
		break;
		
		
		}
		
	}
	
//============Check Existing Semesters=======
	private void checkSemesters(){
		activeSemester=sP.getLong(LeModule.SP_ACTIVESEMESTER, -4);
		Log.i("activeSemester value", String.valueOf(activeSemester));
		//If activeSemester=-4, launch SemesterEdit with the intent of creating a semester for the first time
		if(activeSemester==-4){
			Intent launchSemEditNoSemesters=new Intent(this, ActivitySemesterEdit.class);
			launchSemEditNoSemesters.putExtra("noSemesters", true);
			startActivityForResult(launchSemEditNoSemesters, REQ_LAUNCH_SEMEDIT_NOSEMESTERS);
			
		} else if (!dB.semesterHasClasses(activeSemester)){
		//If we are here, at least 1 semester exists, but no classes exist
			//TODO launch classEdit with NOCLASSES Intent code
			Intent launchClassEditNoClasses= new Intent(ActivitySchooled.this, ActivityClassEdit.class);
			launchClassEditNoClasses.putExtra("semId",activeSemester);
			launchClassEditNoClasses.putExtra("editMode", LeModule.EXTRA_ADDCLASSES_NOCLASSES);
			startActivityForResult(launchClassEditNoClasses, REQ_LAUNCH_CLASSEDIT_NOCLASSES);
			
		} else{
			//if we are here, we are good to go.
			
			
		}
	
		
	}
		
//====================ON ACTIVITY RESULT=======================
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data){
		
		Log.i("Result OK", String.valueOf(RESULT_OK));
		switch(requestCode){
		
		//In this case we have launched semester edit because we didn't find a semester.
		//First, we must set the activeSemester to the newest Semester
		//From this point, we will run the checkExisting() again. The check should find that there are no classes in the current semester
		case REQ_LAUNCH_SEMEDIT_NOSEMESTERS:
			
			
				if(resultCode==RESULT_OK){
					if(sP==null){sP=getSharedPreferences(LeModule.PREFS, Activity.MODE_PRIVATE);}
						SharedPreferences.Editor sEditor= sP.edit();
						dB=GradeDB.getInstance(this);
						Cursor c=dB.fetchAllSemesters();
						sEditor.putLong(LeModule.SP_ACTIVESEMESTER, c.getLong(c.getColumnIndexOrThrow(GradeDB.KEY_SEMID))).commit();
						
						c.close();
						sEditor.apply();
						checkSemesters();
						
						
						
				}else{
					//TODO launch a dialog saying we cannot proceed without a semester.
				}
			break;
		}
	}
	
	
	
	

}
